package sg.edu.nus.iss.cerberus.views;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import sg.edu.nus.iss.cerberus.handler.CreateTestMethodActionExecutor;
import sg.edu.nus.iss.cerberus.util.CerberusConstants;
import sg.edu.nus.iss.cerberus.util.CerberusMethodsDelegate;
import sg.edu.nus.iss.cerberus.wizard.NewTestMethodWizard;

public class CerberusTestGen implements IObjectActionDelegate {

	private ISelection selection;
	private IWorkbenchPart part;

	public void run(IAction action) {
		Object structuredSelection = ((IStructuredSelection) selection)
		.getFirstElement();
		if (structuredSelection instanceof IPackageFragmentRoot) {
			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG_FOR_NEW_TEST_WIZARD);
		}
		else if (structuredSelection instanceof ICompilationUnit) {
			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG_FOR_NEW_TEST_WIZARD);
		} 

	else if (structuredSelection instanceof IPackageFragment) {
		CerberusMethodsDelegate.createPopupDialogue(
				CerberusConstants.WINDOW_TITLE,
				CerberusConstants.UNIMPL_MSG_FOR_NEW_TEST_WIZARD);
	}
	else if (structuredSelection instanceof IProject) {
		CerberusMethodsDelegate.createPopupDialogue(
				CerberusConstants.WINDOW_TITLE,
				CerberusConstants.UNIMPL_MSG_FOR_NEW_TEST_WIZARD);
	}
	else if (selection instanceof IStructuredSelection) {
		// Instantiates and initializes the wizard
		NewTestMethodWizard wizard = new NewTestMethodWizard(part);
//		if ((selection instanceof IStructuredSelection) || (selection == null))
			wizard.init(part.getSite().getWorkbenchWindow().getWorkbench(),
					(IStructuredSelection) selection);

		// Instantiates the wizard container with the wizard and opens it
		WizardDialog dialog = new WizardDialog(part.getSite().getShell(),
				wizard);
		dialog.create();
		dialog.open();
	}
		else  {
			CerberusMethodsDelegate.createPopupDialogue(
					CerberusConstants.WINDOW_TITLE,
					CerberusConstants.UNIMPL_MSG_FOR_NEW_TEST_WIZARD);
		}
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

	public void setActivePart(IAction action, IWorkbenchPart part) {
		this.part = part;
	}
}
